﻿/*
 * @Description:新增楼盘页面js
 * @Author:yangpl
 * @Date:2014-08-18
 */

var CreateEstate = {};
CreateEstate.onBegin = function () {
    other.buttonDisable("#btnSubSaveEstate,#btnSubSaveEstateAndAddBuilding", "disabled");
};

//楼盘名称判重验证
CreateEstate.checkEstateName = function () {
    if ($("#addEstateForm").find("#txtEstateName").next(".wrong").length > 0) {
        $("#addEstateForm").find("#txtEstateName").removeClass("checkError").next(".wrong").remove();
    }

    var estateName = $("#addEstateForm").find("#txtEstateName").val();
    var districtKeyId = $("#addEstateForm").find("#selAllDistrictData").val();
    var areaKeyId = $("#addEstateForm").find("#selGeographyRegionData").val();
    var boardKeyId = $("#addEstateForm").find("#selBlockRegionData").val();
    var businessAreaKeyId = $("#addEstateForm").find("#selBusinessAreaData").val();

    if (estateName.length <= 0 || !districtKeyId || !areaKeyId || !boardKeyId || !businessAreaKeyId) {
        return false;
    }
    var reqData = {
        "EstateName": estateName,
        "DistrictKeyId": districtKeyId,
        "AreaKeyId": areaKeyId,
        "BoardKeyId": boardKeyId,
        "BusinessAreaKeyId": businessAreaKeyId
    };
    $.AjaxAgency("/estate/check-estate-name", reqData, function (data) {
        var $TempCurrentEleTxtEstateName = $("#addEstateForm").find("#txtEstateName");
        if (data == "ok") {
            if ($TempCurrentEleTxtEstateName.next(".wrong").length > 0) {
                $TempCurrentEleTxtEstateName.removeClass("checkError").next(".wrong").remove();
            }
        } else if (data == "no") {

            if ($TempCurrentEleTxtEstateName.next(".wrong").length <= 0) {
                $TempCurrentEleTxtEstateName.addClass("checkError").after(agencyVerify.greatErrorHtml($TempCurrentEleTxtEstateName, "含有相同名称的楼盘名称"));

            }
        } else {
            $.dialog({
                lock: true,
                background: "#808080",
                opacity: 0.87,
                content: data,
                cancel: function () { }
            });
        }
    });
};

//获取片区，板块，商圈数据
CreateEstate.getEstateLinkData = function (isLoadBlock, iaLoadRegion) {
    $("#selBusinessAreaData").html("");
    if (isLoadBlock) {
        $("#selBlockRegionData").html("");
    }
    if (iaLoadRegion) {
        $("#selGeographyRegionData").html("");
    }
    var tempAllDistrict = $("#selAllDistrictData").val();
    var centaNetDistrict = $("#selAllCentaNetDistrictData").val();
    if (centaNetDistrict == "" || centaNetDistrict == null) {
        centaNetDistrict = "00000000-0000-0000-0000-000000000000";
    }
    var reqData = {
        "DistrictKeyId": tempAllDistrict,
        "CentaNetDistrictKeyId": centaNetDistrict
    };
    $.AjaxAgency("/estate/get-create-estate-link-data", reqData, function (data) {
        if (typeof data == "object") {
            if (iaLoadRegion) {
                if (data.GeographyRegion.Flag) {
                    var tempHtml = "", tempCount = 0;
                    for (var i = 0; i < data.GeographyRegion.Result.length; i++) {
                        tempHtml += '<option value="' + data.GeographyRegion.Result[i].KeyId + '">' + data.GeographyRegion.Result[i].RegionName + '</option>';
                        tempCount = i + 1;
                    }
                    if (tempCount > 0) {
                        var tempEle = $("#selGeographyRegionData");
                        var wrongspan = tempEle.next();
                        if (wrongspan.attr("class") == "wrong") {
                            wrongspan.remove();
                            tempEle.removeClass("checkError");

                        }
                    }
                    $("#selGeographyRegionData").append(tempHtml);
                }
            }
            if (isLoadBlock) {
                if (data.BlockRegion.Flag) {
                    var tempHtml = "<option value=''>请选择</option>", tempCount = 0;
                    for (var i = 0; i < data.BlockRegion.Result.length; i++) {
                        tempHtml += '<option value="' + data.BlockRegion.Result[i].KeyId + '">' + data.BlockRegion.Result[i].RegionName + '</option>';
                        tempCount = i + 1;
                    }
                    if (tempCount > 0) {
                        var tempEle = $("#selBlockRegionData");
                        var wrongspan = tempEle.next();
                        if (wrongspan.attr("class") == "wrong") {
                            wrongspan.remove();
                            tempEle.removeClass("checkError");

                        }
                    }
                    $("#selBlockRegionData").append(tempHtml);
                    //$("#selBlockRegionData").change();
                }
            }

            if (data.BusinessArea.Flag) {
                var tempHtml = "", tempCount = 0;
                for (var i = 0; i < data.BusinessArea.Result.length; i++) {
                    tempHtml += '<option value="' + data.BusinessArea.Result[i].KeyId + '">' + data.BusinessArea.Result[i].RegionName + '</option>';
                    tempCount = i + 1;
                }
                if (tempCount > 0) {
                    var tempEle = $("#selBusinessAreaData");
                    var wrongspan = tempEle.next();
                    if (wrongspan.attr("class") == "wrong") {
                        wrongspan.remove();
                        tempEle.removeClass("checkError");

                    }
                }
                $("#selBusinessAreaData").append(tempHtml);
            }

            CreateEstate.checkEstateName();
        }
    });
};

CreateEstate.getEstateDistrictTypeData = function () {
    $("#selDistrictTypeData").html('');
    var tempAllDistrict = $("#selAllDistrictData").val();
    var reqData = { "DistrictKeyIdAll": tempAllDistrict };
    //$("#selDistrictTypeData").html('<option value="-1">-请选择-</option>');
    $.AjaxAgency("/estate/est-dis-districttype-json", reqData, function (res) {
        if (res.Flag) {
            var data = res.Result;
            var tempHtml = "", tempCount = 0;
            for (var i = 0; i < data.length; i++) {
                tempHtml += '<option value="' + data[i].DistrictTypeKeyId + '">' + data[i].DistrictTypeName + '</option>';
            }
            $("#selDistrictTypeData").append(tempHtml);
        }
    });
}

//创建别名
CreateEstate.addEstateAlias = function () {
    var spanEstateAlias = $("#divDelinfo").find(".spanEstateAlias");
    if (spanEstateAlias.length >= 10) {
        $.OKDialogAgency('<div class="tc">别名数量不能大于10！</div>');
        return;

    }
    $("#divDelinfo").append('<span class="spanEstateAlias mb20 l" style="height:26px;margin-right:9px;"><strong class="public h26lh26" style="width:76px;"><em class="star">*</em>别　　名：</strong><input type="text" style="width:85px;" class="ipt" name="EstateAlias" notnull="1" maxlength="30"/><i class="delinfo" onclick="$(this).parent().remove();" title="删除楼盘"></i></span>');
};
//创建学校
CreateEstate.AddEstaeSchool = function (data) {
    var spanEstateSchool = $("#divSchoolInfoBox").find(".spanEstateSchool");
    if (spanEstateSchool.length >= 20) {
        $.OKDialogAgency('<div class="tc">学校数量不能大于20！</div>');
        return;
    }
    var valueKey = "";
    var valueName = "";
    if (data) {
        if (data.valueKey && data.valueKey.length > 0)
            valueKey = data.valueKey;
        if (data.valueName && data.valueName.length > 0)
            valueName = data.valueName;
    }
    var estateSchool =
        $('<span class="l mr10 temp spanEstateSchool mb20">'
        + '<input type="hidden" name="EducationSchoolKeyId" value="' + valueKey + '" />'
        + '<input type="text" class="ipt" name="EducationSearchValue"  maxlength="20" value="' + valueName + '" />'
        + '<i class="delinfo" onclick="$(this).parent().remove();" title="删除学校"></i></span>');
    var isok = $("#divSchoolInfoBox").append(estateSchool);
    pageNewEditEstate.searchEstateSchoolAutoMatch(estateSchool.find("input[name='EducationSearchValue']"));
};

//创建地铁线路、站点
CreateEstate.AddSubWay = function (data) {
    var spanSubway = $("#divSubwayBox").find(".spanSubwayItem");
    if (spanSubway.length >= 10) {
        $.OKDialogAgency('<div class="tc">临近地铁线路站点不能大于10！</div>');
        return;
    }
    var temSubwayHtml = $("#tempSubwayItem").html();
    var obj = $(temSubwayHtml);
    $("#divSubwayBox").append(obj);

    var trap = obj.find("select[name='SubwayKeyId']");
    var trapc = obj.find("select[name='SubwayStationKeyId']");
    pageNewEditEstate.TransportationKeyId(trap);
    trap.bind("change", function () {
        pageNewEditEstate.TransportationItemKeyId(trapc, $(this));
    });
}
CreateEstate.success = function (data) {
    if (data.indexOf("[提示]") == -1) {
        var tempStr = data.split("#");
        if (tempStr[1] == "保存") {
            $.dialog({
                id: "dialogAddEstateInfo",
                lock: true,
                title: "提示信息",
                cancel: false,
                content: '<div><a class="btn-red" href="/estate/create-estate">继续新增</a>&nbsp;<a class="btn-gray" href="javascript:window.opener=null;window.open(\'\',\'_self\');window.close();">关闭当前页</a></div>'
            });

        } else if (tempStr[1] == "增加栋座") {
            window.location.href = "/estate/add-building?EstateKeyId=" + tempStr[2];
        }

    } else {
        $.dialog({
            lock: true,
            background: "#808080",
            opacity: 0.87,
            content: data,
            cancel: function () {
                other.buttonStart("#btnSubSaveEstate,#btnSubSaveEstateAndAddBuilding", "disabled");
            }
        });
    }

};

//获取大楼盘
CreateEstate.getBigHouseData = function () {
    $("#selBigHouseData").html("");
    var tempKeyId = $("#selBlockRegionData").val();
    if (!tempKeyId || tempKeyId.length < 10) {
        return;
    }
    var reqData = {
        "EstateTagTypeCode": "20",
        "DistrictKeyId": $("#selAllDistrictData").val(),
        "RegionKeyId": tempKeyId
    };
    $.AjaxAgency("/estate/get-bighouse-bydis-reg", reqData, function (data) {
        if (typeof data == "object") {
            var tempHtml = '<option value="-1">请选择</option>';
            for (var i = 0; i < data.length; i++) {
                tempHtml += '<option value="' + data[i].KeyId + '">' + data[i].EstateTagName + '</option>';
            }
            $("#selBigHouseData").append(tempHtml);
            $("#selBigHouseData").val($("#selBigHouseData").attr("bandingsel"));

        } else if (data == "ok") {

        } else {
            $.dialog({
                lock: true,
                background: "#808080",
                opacity: 0.87,
                content: data,
                cancel: function () { }
            });
        }
    });
};

CreateEstate.getDwellingSubType = function () {
    $("#selDwellingSubType").html("");
    var tempKeyId = $("#selDwellingType").val();
    if (!tempKeyId || tempKeyId.length < 10) {
        return;
    }
    var reqData = {
        "KeyId": $("#selDwellingType").val()
    };
    $.AjaxAgency("/estate/find-sys-parameter-dwellingsubtype", reqData, function (data) {
        if (typeof data == "object") {
            var tempHtml = '<option value="-1">请选择</option>';
            for (var i = 0; i < data.length; i++) {
                tempHtml += '<option value="' + data[i].ItemValue + '">' + data[i].ItemText + '</option>';
            }
            $("#selDwellingSubType").append(tempHtml);
            $("#selDwellingSubType").val($("#selDwellingSubType").attr("bandingsel"));

        } else if (data == "ok") {

        } else {
            $.dialog({
                lock: true,
                background: "#808080",
                opacity: 0.87,
                content: data,
                cancel: function () { }
            });
        }
    });
};

//初始化方法
CreateEstate.init = function () {
    //勾选是否官网显示需要验证官网行政区和板块死活必填项
    $("#addEstateForm input[name=IsWebSiteDisplay]").click(function () {
        if ($(this).attr("checked") == "checked") {
            $("#selAllCentaNetDistrictData").attr('notnull', '1');
            $("#selBlockRegionData").attr('notnull', '1');
        }
        else {
            $("#selAllCentaNetDistrictData").removeAttr('notnull');
            //移除必填项的样式
            if ($("#selAllCentaNetDistrictData").next(".wrong").length > 0) {
                $("#selAllCentaNetDistrictData").removeClass("checkError").next(".wrong").remove();
            }
            $("#selBlockRegionData").removeAttr('notnull');
            if ($("#selBlockRegionData").next(".wrong").length > 0) {
                $("#selBlockRegionData").removeClass("checkError").next(".wrong").remove();
            }
        }
    });

    $.inputStatistics("txtRemark", 200);
    //初始化片区，板块，商圈数据 
    CreateEstate.getEstateLinkData(true, true);
    CreateEstate.getEstateDistrictTypeData();
    //官网行政区联动板块
    $("#selAllCentaNetDistrictData").change(function () {
        CreateEstate.getEstateLinkData(true, false);
    });
    //A+行政区联动片区，行政区类型
    $("#selAllDistrictData").change(function () {
        CreateEstate.getEstateLinkData(false, true);
        CreateEstate.getEstateDistrictTypeData();
    });
    $("#selDwellingType").change(function () {
        CreateEstate.getDwellingSubType();
    });
    $("#selBlockRegionData").change(function () { CreateEstate.getBigHouseData(); });
    var tempPropertyUsageData = dataBind.getRealDataByType(3), tempPropertyTypeData = dataBind.getRealDataByType(4), tempPropertyRightNatureKeyIdData = dataBind.getRealDataByType(53);

    $("#selPropertyRightNatureKeyId").append('<option value="-1">--请选择--</option>');
    for (var j = 0; j < tempPropertyRightNatureKeyIdData.length; j++) {
        $("#selPropertyRightNatureKeyId").append('<option value="' + tempPropertyRightNatureKeyIdData[j].ItemValue + '">' + tempPropertyRightNatureKeyIdData[j].ItemText + '</option>');

    }

    for (var i = 0; i < tempPropertyUsageData.length; i++) {
        $("#selPropertyUsageData").append('<option value="' + tempPropertyUsageData[i].ItemValue + '">' + tempPropertyUsageData[i].ItemText + '</option>');
    }
    for (var i = 0; i < tempPropertyTypeData.length; i++) {
        $("#selPropertyTypeData").append('<option value="' + tempPropertyTypeData[i].ItemValue + '">' + tempPropertyTypeData[i].ItemText + '</option>');
    }
};